/*
      ____                      _____                  +---+
     / ___\                     / __ \                 | R |
    / /                        / /_/ /                 +---+
   / /   ________  ____  ___  / ____/___  ____  __   __
  / /  / ___/ __ `/_  / / _ \/ /   / __ \/ _  \/ /  / /
 / /__/ /  / /_/ / / /_/  __/ /   / /_/ / / / / /__/ /
 \___/_/   \__,_/ /___/\___/_/    \___ /_/ /_/____  /
                                                 / /
                                            ____/ /
                                           /_____/
main.c file
编写者：小马  (Camel)
作者E-mail：375836945@qq.com
编译环境：MDK-Lite  Version: 4.23
初版时间: 2014-01-28
功能：
------------------------------------
*/
#include "config.h"             //包含所有的驱动头文件
#include "led.h"

#define RCC_APB2ENR (0x40021000 + 0x18)

#define IOPAEN 2
#define IOPBEN 3
#define GPIOA_CRL (0x40010800 + 0)
#define GPIOB_CRH (0x40010C00 + 4)

#define PA_BASE (0x40010800)
#define PB_BASE (0x40010C00)
#define GPIO_A_ODR (PA_BASE + 0x0C)
#define GPIO_B_ODR (PB_BASE + 0x0C)

#define PB13 (0x42000000+(GPIO_B_ODR - 0x40000000)*32 + 13*4)
#define PB14 (0x42000000+(GPIO_B_ODR - 0x40000000)*32 + 14*4)
#define PB15 (0x42000000+(GPIO_B_ODR - 0x40000000)*32 + 15*4)

#define RED 0
#define BLUE 1
#define GREEN 2
#define FULL 3

#define UART_DEBUG


void led_init()
{
	unsigned int *pa = (unsigned int *)RCC_APB2ENR;
	unsigned int tempa = *pa;
	unsigned int *pb = (unsigned int *)RCC_APB2ENR;
	unsigned int tempb = *pb;
	
	tempa |= 3 << IOPAEN;
	*pa = tempa;
	
	pa = (unsigned int *)GPIOA_CRL;
	tempa = *pa;
	tempa = 0;
	tempa = 0x33333333;
	*pa = tempa;
	

	pb = (unsigned int *)GPIOB_CRH;
	tempb = *pb;
	tempb &= ~(0xFFF << 20);
	tempb |= 0x333 << 20;
	*pb = tempb;
}



void array_led(unsigned char byte)
{
	unsigned int *p;
	unsigned int temp;
	p = (unsigned int *)GPIO_A_ODR;
	temp = *p;
	temp = (~(byte & 0x00ff)) & 0x00ff;
	*p = temp;
}

void switch_color(unsigned int flag, unsigned int rgb)
{
	volatile unsigned int *pb13 = (volatile unsigned int *)PB13; 
	volatile unsigned int *pb14 = (volatile unsigned int *)PB14;
	volatile unsigned int *pb15 = (volatile unsigned int *)PB15;
	if(flag == 0)
	{
		switch(rgb)
		{
			case RED :
			{				
				*pb13 = 0;
				*pb14 = 1;
				*pb15 = 1;
				break;
			}
			case GREEN :
			{
				*pb13 = 1;
				*pb14 = 0;
				*pb15 = 1;
				break;
			}
			case BLUE :
			{
				*pb13 = 1;
				*pb14 = 1;
				*pb15 = 0;
				break;
			}
			case FULL :
			{
				*pb13 = 0;
				*pb14 = 0;
				*pb15 = 0;
				break;
			}
		}
	}
	else
	{
		*pb13 = 1;
		*pb14 = 1;
		*pb15 = 1;
	}
}

void delay(int s)
{
	while(s--)
	{
		int i = 1000;
		while(i--);
	}
}

void led_on(unsigned int i, unsigned int flag, unsigned int rgb)
{
	volatile unsigned int *p = (volatile unsigned int *)(0x42000000+(GPIO_A_ODR - 0x40000000)*32 + i * 4);
	*p = 0;
	switch_color(0, rgb);
}

void led_off(unsigned int i, unsigned int flag, unsigned int rgb)
{
	volatile unsigned int *p = (volatile unsigned int *)(0x42000000+(GPIO_A_ODR - 0x40000000)*32 + i * 4);
	*p = 1;
	switch_color(0, rgb);
}

void led_mode1()
{
	unsigned int i = 0;
	for(i = 0; i < 8; i++)
	{
		led_on(i, 0, RED);
		delay(400);
		led_off(i, 1, RED);
		delay(400);
	}
}

void led_mode2()
{
	unsigned int i = 0;
	i = 7;
	while(i--)
	{
		led_on(i, 0, GREEN);
		delay(600);
		led_off(i, 1, GREEN);
		delay(600);
	}
}

void led_mode3()
{
	unsigned int j = 0;
	unsigned int i = 0;
	
	for(i = 0, j = 7; i < j; i++, j--)
	{
		led_on(i, 0, GREEN);
		delay(500);
		led_off(i, 1, GREEN);
		led_on(j, 0, BLUE);
		delay(500);
		led_off(j, 1, BLUE);
	}
}


void led_mode4()
{
	unsigned int j = 0;
	unsigned int i = 0;
	
	for(i = 3, j = 4; i >= 0, j < 8; i--, j++)
	{
		led_on(i, 0, BLUE);
		led_on(j, 0, RED);
		delay(1000);
		led_off(i, 1, BLUE);
		led_off(j, 1, RED);
		delay(1000);
	}
}


int main(void)
{
	uint8_t temp;

	uint8_t data[4] = {0};
	int num;
	int i = 0;
	int status = 0;

	
	
	
	led_init();
	/***********************************/
	SystemClock_HSI(9);           //系统时钟初始化，时钟源内部HSI
	
	cycleCounterInit();				    // Init cycle counter
	SysTick_Config(SystemCoreClock / 1000);	//SysTick开启系统tick定时器并初始化其中断，1ms
	UART1_init(SysClock,uart1baudSet); //串口1初始化
  NVIC_INIT();	                //中断初始化
	//KeyInit();										//按键初始化
	LedInit();
#ifdef UART_DEBUG  
	TIM3_Init(SysClock,300);			//定时器初始化，1s为周期打印摇杆值
#endif
	TIM4_Init(SysClock,TIME4_Preiod);	  //定时器4初始化，定时时间单位：(TIME4_Preiod)微秒


	while(1)             
	{
		if(flag10Hz)
		{
			flag10Hz = 0;
			LedSet(led6,status);
			
			if(status)
				status = 0;
			else
				status = 1;
		
			if(USART1_bytesAvailable() != 0)
			{
				
				UartBufClear(&UartTxbuf);
				//UartBufClear(&UartRxbuf);
				
				temp = USART1_Getchar();
				printf("num is : %d\r\n", sizeof(temp));
				printf("Data is : %c\r\n", temp);

				switch(temp)
				{
					case '1' :
					{
						printf("led_mode1() is running !\r\n");
						led_mode1();
						break;
					}
					case '2' :
					{
						printf("led_mode2() is running !\r\n");
						led_mode2();
						break;
					}
					case '3' :
					{
						printf("led_mode3() is running !\r\n");
						led_mode3();
						break;
					}
					case '4' :
					{
						printf("led_mode4() is running !\r\n");
						led_mode4();
						break;
					}
					default: break;
				}

				
				UartSendBuffer(&temp, sizeof(temp));	
			}	
		}
		
	}
}

